package org.example.student_dormitory.controller;

import net.sf.json.JSONObject;
import org.example.student_dormitory.dto.Result;
import org.example.student_dormitory.service.DormitoryService;
import org.example.student_dormitory.service.impl.DormitoryServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet("/dormitory")
public class DormitoryServlet extends HttpServlet {
    private DormitoryService dormitoryService = new DormitoryServiceImpl();

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        // 设置请求响应编码
        req.setCharacterEncoding("utf-8");
        resp.setCharacterEncoding("utf-8");
        resp.setContentType("application/json");

        // 获取前端请求数据
        String method = req.getParameter("method");
        switch (method){
            case "paging" ://分页
                paging(req,resp);
                break;
            case "loadBuilding"://加载楼栋
                loadbuildings(req,resp);
                break;
            case "findDormitoryName":
                findDormitoryName(req,resp);
                break;
            case "add":
                addDormitory(req,resp);
                break;
            case "edit":
                editDormitory(req,resp);
                break;
            case "delete" ://删除
                deleteAdmin(req,resp);
                break;
        }
    }

    private void deleteAdmin(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        String id = req.getParameter("id");
        Result result = dormitoryService.deleteById(id);
        returnData(resp,result);
    }

    private void editDormitory(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        String id = req.getParameter("id");
//        String building = req.getParameter("building");
        String dormitoryName = req.getParameter("dormitoryName");
        String typeNumber = req.getParameter("typeNumber");
        String spareBed = req.getParameter("spareBed");
        System.out.println(1);
        Result result = dormitoryService.updateDormitory(id, dormitoryName, spareBed, typeNumber);
        returnData(resp,result);
    }

    private void addDormitory(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        String name = req.getParameter("name");
        String buildingId = req.getParameter("buildingId");
        String dormitoryType = req.getParameter("dormitoryType");
        Result result = dormitoryService.addDormitory(name, buildingId, dormitoryType);
        returnData(resp,result);
    }

    private void findDormitoryName(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        //校验宿舍号是否可用
        String buildingId = req.getParameter("buildingId");
        String name = req.getParameter("name");
        Result result = dormitoryService.checkDormitoryName(name, buildingId);
        returnData(resp,result);
    }

    private void loadbuildings(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        //加载楼栋信息
        Result result = dormitoryService.loadBuildings();
        returnData(resp,result);
    }

    private void paging(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        String page = req.getParameter("page");
        String pageSize = req.getParameter("pageSize");
        String key = req.getParameter("key");
        String value = req.getParameter("value");

//        System.out.println("1准备进入Service");
        Result result = dormitoryService.loadDormitory(page,pageSize,key,value);
//        System.out.println("7拿到最终result,准备转给前端");
        returnData(resp,result);
    }

    private void returnData(HttpServletResponse resp, Result result) throws IOException {
        String json_str = JSONObject.fromObject(result).toString();

        resp.getWriter().write(json_str);
    }
}
